package action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import entity.Answer;
import entity.Question;
import entity.Survey;
import entity.User;
import service.AnswerService;
import service.QuestionService;
import service.SurveyService;

@Controller
public class HomeAction {
	@Autowired
	private SurveyService surveyService;
	
	@Autowired
	private QuestionService questionService;
	
	@Autowired
	private AnswerService answerService;
	
	@RequestMapping("/newSurveyView.do")
	public ModelAndView welcome(String survey_name,String survey_endTime,String survey_other,HttpServletRequest request){
		return new ModelAndView("newSurvey");
	}
	
	@RequestMapping("/newSurvey.do")
	public String newSurvey(String survey_name,String survey_endTime,String survey_other,HttpServletRequest request) {
		Survey survey = new Survey();
		User user = new User();
		HttpSession session = request.getSession();
		user = (User) session.getAttribute("users");
		
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		
		survey.setSurvey_name(survey_name);
		survey.setSurvey_startTime(df.format(new Date()));
		survey.setSurvey_endTime(survey_endTime);
		survey.setSurvey_state(1);
		survey.setUser_name(user.getUser_name());
		survey.setSurvey_other(survey_other);
		
		surveyService.newSurvey(survey);
		Survey surveys = surveyService.findSurveyByName(survey);
		int survey_id = surveys.getSurvey_id();
		return "redirect:/newQuestionView.do?survey_id=" + survey_id;
	}
	
	@RequestMapping("/newQuestionView.do")
	public ModelAndView newQuestionView(HttpServletRequest request) {
		User user = new User();
		HttpSession session = request.getSession();
		user = (User) session.getAttribute("users");
		if(request.getParameter("survey_id")!=null) {
			String surveyId=request.getParameter("survey_id");
			Map<String,Object> model = new HashMap<String,Object>();
			Survey survey = new Survey();
			survey.setSurvey_id(Integer.parseInt(surveyId));
			survey.setUser_name(user.getUser_name());
			model.put("survey", survey);
			return new ModelAndView("newQuestion",model);
		}else {
			return new ModelAndView("newQuestion");
		}
		
	}
	
	@RequestMapping(value="/newQuestion.do",method = RequestMethod.POST)
	public String newQuestion(String survey_questionName,int survey_questionType,int survey_id,
			String survey_questionSingleA,String survey_questionSingleB,String survey_questionSingleC,
			String survey_questionAnswer,HttpServletRequest request) {
		System.out.println(survey_questionName);
		System.out.println(survey_questionType);
		System.out.println(survey_id);
		System.out.println(survey_questionSingleA);
		System.out.println(survey_questionSingleB);
		System.out.println(survey_questionSingleC);
		System.out.println(survey_questionAnswer);
		System.out.println(request);
		
		Question question = new Question();
		question.setSurvey_questionName(survey_questionName);
		question.setSurvey_questionType(survey_questionType);
		question.setSurvey_id(survey_id);
		if(survey_questionType==1) {
			question.setSurvey_questionSingleA(survey_questionSingleA);
			question.setSurvey_questionSingleB(survey_questionSingleB);
			question.setSurvey_questionSingleC(survey_questionSingleC);
		}else {
			question.setSurvey_questionAnswer(survey_questionAnswer);
		}
		questionService.newQuestion(question);
		
		if(request.getParameter("Action").equals("继续添加")) {
			System.out.println("继续提交");
			return "redirect:/newQuestionView.do?survey_id=" + survey_id;
		}else if(request.getParameter("Action").equals("保存问卷")) {
			System.out.println("保存问卷");
			return "redirect:/home.do";
		}
		return "redirect:/home.do";
	}
	
	@RequestMapping("/surveyDelet.do")
	public String surveyDelet(HttpServletRequest request) {
		Survey survey = new Survey();
		Question question = new Question();
		String surveyIdTemp=request.getParameter("survey_id");
		int surveyId=Integer.parseInt(surveyIdTemp);
		
		survey.setSurvey_id(surveyId);
		question.setSurvey_id(surveyId);
		
		System.out.println(surveyId);
		System.out.println(survey.getSurvey_id());
		System.out.println(question.getSurvey_id());
		surveyService.deleteSurveyById(survey);
		questionService.deleteQuestionById(question);
		
		return "redirect:/home.do";
	}
	
	@RequestMapping("/allSurvey.do")
	public ModelAndView allSurvey() {
		Map<String,Object> model = new HashMap<String,Object>();
		List<Survey> surveys = surveyService.findAllSurveys();
		if(surveys.size()==0) {
			return new ModelAndView("allSurvey");
		}
		model.put("surveys", surveys);
		return new ModelAndView("allSurvey",model);
	}
	
	@RequestMapping("/writeSurveyView.do")
	public ModelAndView writeSurveyView(HttpServletRequest request) {
		System.out.println(request.getParameter("survey_id"));
		
		String surveyIdTemp=request.getParameter("survey_id");
		int surveyId=Integer.parseInt(surveyIdTemp);
		Survey surveyTemp = new Survey();
		surveyTemp.setSurvey_id(surveyId);
		Survey survey= surveyService.findSurveyById(surveyTemp);
		System.out.println(survey.getSurvey_name());
		
		Question questionTemp = new Question();
		questionTemp.setSurvey_id(surveyId);
		List<Question> questions= questionService.findQuestionBySurveyId(questionTemp);
		
		Map<String,Object> model = new HashMap<String,Object>();
		model.put("survey", survey);
		if(questions.size()==0) {
			return new ModelAndView("writeSurvey",model);
		}else {
			model.put("questions", questions);
		}
		return new ModelAndView("writeSurvey",model);
	}
	
	@RequestMapping(value="/writeSurvey.do",method = RequestMethod.POST)
	public String writeSurvey(String[] survey_questionSingle,String[] survey_questionAnswer,HttpServletRequest request) {
		Enumeration<String> parameterNames = request.getParameterNames();
		Question question = new Question();
		Answer answers = new Answer();
		int count = 0;
		int surveyId=0;
        while(parameterNames.hasMoreElements()){
            String param = parameterNames.nextElement();
            if(param.startsWith("survey_questionSingle")){
                String id = param.substring("survey_questionSingle".length());   //选择题id
                String answer = request.getParameter(param);
                question.setSurvey_questionId(Integer.parseInt(id));
                question = questionService.findQuestionByQuestionId(question);
                surveyId=question.getSurvey_id();
                System.out.println(question.getSurvey_questionName());
                System.out.println(question.getSurvey_questionSingleA().equals(answer));
                if(question.getSurvey_questionSingleA().equals(answer)) {
                		count = question.getSurvey_singleACount()+1;
                		System.out.println(count);
                		question.setSurvey_singleACount(count);
                }else if(question.getSurvey_questionSingleB().equals(answer)) {
	                	count = question.getSurvey_singleBCount()+1;
	            		System.out.println(count);
	            		question.setSurvey_singleBCount(count);
                }else if(question.getSurvey_questionSingleC().equals(answer)) {
	                	count = question.getSurvey_singleCCount()+1;
	            		System.out.println(count);
	            		question.setSurvey_singleCCount(count);
                }
                questionService.updateQuestionByQuestionId(question);
                question= new Question();
                System.out.println(id+"---"+answer);
            }
            if(param.startsWith("survey_questionAnswer")){
                String id = param.substring("survey_questionAnswer".length());   //选择题id
                String answer = request.getParameter(param); 
                answers.setSurvey_questionId(Integer.parseInt(id));
                answers.setAnswer_infor(answer);
                answerService.updateAnswerByQuestionId(answers);
                System.out.println(id+"---"+answer);
            }
        }
        Survey tmp = new Survey();
        tmp.setSurvey_id(surveyId);
        tmp = surveyService.findSurveyById(tmp);
        count = tmp.getSurvey_statistics()+1;
        System.out.println(count);
        tmp.setSurvey_statistics(count);
        surveyService.updateSurveyBySurveyId(tmp);
		return "redirect:/allSurvey.do"; 
	}
	
	
	@RequestMapping("/showResultView.do")
	public ModelAndView showResultView(HttpServletRequest request) {
		String surveyIdTemp=request.getParameter("survey_id");
		int surveyId=Integer.parseInt(surveyIdTemp);
		Question question = new Question();
		question.setSurvey_id(surveyId);
		List<Question> questions= questionService.findQuestionBySurveyId(question);
		
		Map<String,Object> model = new HashMap<String,Object>();
		if(questions.size()==0) {
			return new ModelAndView("writeSurvey",model);
		}else {
			model.put("questions", questions);
		}
		return new ModelAndView("showResult",model);
	}
	
}
